package org.atf.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.atf.model.entity.Preference;
import org.atf.model.entity.Profile;
import org.atf.model.entity.Server;
import org.atf.model.utils.EntityManagerUtil;

public class PreferenceDAO {
	
	public List<Preference> getAllPreferences(){	
		EntityManager entityManager=EntityManagerUtil.getEntityManager();
		Query query = entityManager.createQuery("select p from Preference p");
		List<Preference> resultList = query.getResultList();
		return resultList;
	}
	public void insertPreference(Preference preference){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
			entityManager.persist(preference);
			entityManager.getTransaction().commit();
		}finally {
			//entityManager.close();
		}
	}
	public void updatePreference(Preference preference){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
			entityManager.merge(preference);
			entityManager.getTransaction().commit();
		}finally {
			//entityManager.close();
		}
		
	}
	public List<Preference> getAllPreferenceByScope(String scope) {
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		List<Preference> prefList = new ArrayList<Preference>();
		try{
			prefList =
					entityManager.createNamedQuery("Preference.findPreferenceByScope")
					.setParameter("scope", scope)
					.getResultList();
		}finally {
			//entityManager.close();
		}
		return prefList;
	}
	public Preference getPreference(Integer id){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		Preference preference;
		 try{
				entityManager.getTransaction().begin();
				preference=entityManager.find(Preference.class, id);
				entityManager.getTransaction().commit();
		 } finally {
			// entityManager.close();
		 }	
		 return preference;
	}
	public void deletePreference(Integer id){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		 try{
				entityManager.getTransaction().begin();
				Preference preference=entityManager.find(Preference.class, id);
				entityManager.remove(preference);
				entityManager.flush();
				entityManager.getTransaction().commit();
		 } finally {
			// entityManager.close();
		 }	
	}
	
	public Preference getPreferenceByName(String pName){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		Preference pref = null;
		try{
			pref =
					entityManager.createNamedQuery("Preference.findPreferenceByName", Preference.class)
					.setParameter("name", pName)
					.getSingleResult();
		}catch (NoResultException e){
			pref= null;
		}finally {
			//entityManager.close();
		}
		return pref;
	}
	
	public Preference getPreferenceByNameScope(String pName, String scope){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		Preference pref = null;
		try{
			List<Preference> prefList =
					entityManager.createNamedQuery("Preference.findPreferenceByNameScope", Preference.class)
					.setParameter("name", pName)
					.setParameter("scope", scope)
					.getResultList();
			if(!prefList.isEmpty()){
				pref=prefList.get(0);
			}
		}finally {
			//entityManager.close();
		}
		return pref;
	}
	
	public Preference getPreferenceByName(Integer profileId,Integer serverId,String name, String scope){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		Preference preference= null;
		
		if(serverId > 0 ){
			String sqlQuery = "SELECT P.* FROM SERVER_PREFERENCE SP, PREFERENCE P "
					+ " WHERE SP.SERVER_ID = ? AND SP.PREFERENCE_ID =P.PREFERENCE_ID "
					+ " AND P.PNAME=?";
			try {
				Query query =entityManager.createNativeQuery(sqlQuery, Preference.class);
				query.setParameter(1, serverId);
				query.setParameter(2, name);
				preference = (Preference) query.getSingleResult();
			}catch(NoResultException e){
				preference= null;
			}
	
		}
		if(profileId > 0 && preference == null){
			String sqlQuery = "SELECT P.* FROM PROFILE_PREFERENCE PP, PREFERENCE P "
					+ " WHERE PP.PROFILE_ID = ? AND PP.PREFERENCE_ID =P.PREFERENCE_ID "
					+ " AND P.PNAME=?";
			try {
				Query query =entityManager.createNativeQuery(sqlQuery, Preference.class);
				query.setParameter(1, profileId);
				query.setParameter(2, name);
				preference = (Preference) query.getSingleResult();
			}catch(NoResultException e){
				preference= null;
			}
		}
		
		if(preference == null){
			preference= getPreferenceByNameScope(name, "GLOBAL");
		}
		
		return preference;
		
	}
	
	public Preference getPreferenceByJobScope(Integer jobId, String name,String scope){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		Preference preference= null;
		String sqlQuery = "SELECT P.* FROM JOBS_PREFERENCE JP, PREFERENCE P "
				+ " WHERE JP.JOB_ID = ? AND JP.PREFERENCE_ID =P.PREFERENCE_ID "
				+ " AND P.PNAME=?";
		try {
			Query query =entityManager.createNativeQuery(sqlQuery, Preference.class);
			query.setParameter(1, jobId);
			query.setParameter(2, name);
			preference = (Preference) query.getSingleResult();
		}catch(NoResultException e){
			preference= null;
		}
		if(preference == null){
			preference= getPreferenceByNameScope(name, "GLOBAL");
		}
		return preference;
	}
	public void addOrUpdateDefaultsPreference() {
		// TODO Auto-generated method stub
		
		if( getPreferenceByNameScope("OS_VERSION", "GLOBAL") == null){
			insertPreference(new Preference("OS_VERSION", "AIX", "OS VERSION","GLOBAL"));
		}
		if( getPreferenceByNameScope("SSH_ENABLED", "GLOBAL") == null){
			insertPreference(new Preference("SSH_ENABLED", "TRUE", "SSH Enabled for remote connection","GLOBAL"));
		}
		if( getPreferenceByNameScope("SSH_USERNAME", "GLOBAL") == null){
			insertPreference(new Preference("SSH_USERNAME", "", "SSH USERNAME","GLOBAL"));
		}
		if( getPreferenceByNameScope("SSH_PASSWORD", "GLOBAL") == null){
			insertPreference(new Preference("SSH_PASSWORD", "", "SSH PASSWORD.  Please use Admin Utils page to encypt the password","GLOBAL"));
		}
		if( getPreferenceByNameScope("SSH_PORT", "GLOBAL") == null){
			insertPreference(new Preference("SSH_PORT", "22", "SSH Port ","GLOBAL"));
		}
		if( getPreferenceByNameScope("SSH_KNOWN_HOST_FILE", "GLOBAL") == null){
			insertPreference(new Preference("SSH_KNOWN_HOST_FILE", System.getProperty("user.home")+"/.ssh/known_hosts", "ssh known host file name","GLOBAL"));
		}
		
		if( getPreferenceByNameScope("TEMP_DIR", "GLOBAL") == null){
			insertPreference(new Preference("TEMP_DIR", "/tmp", "Temporary Directory","GLOBAL"));
		}
		if( getPreferenceByNameScope("SCRIPT_PATH", "GLOBAL") == null){
			insertPreference(new Preference("SCRIPT_PATH", "/apps/scripts/v70", "Default Script locations","GLOBAL"));
		}
		if( getPreferenceByNameScope("DEPLOY_SCRIPT", "GLOBAL") == null){
			insertPreference(new Preference("DEPLOY_SCRIPT", "", "Full Deployment script","GLOBAL"));
		}
		if( getPreferenceByNameScope("CONFIG_SCRIPT", "GLOBAL") == null){
			insertPreference(new Preference("CONFIG_SCRIPT", "", "Full Configuration script","GLOBAL"));
		}
		if( getPreferenceByNameScope("SCRIPT_SUDO", "GLOBAL") == null){
			insertPreference(new Preference("SCRIPT_SUDO", "sudo -u wasadmin", "default sudo to run scripts","GLOBAL"));
		}
		if( getPreferenceByNameScope("TAM_ENABLED", "GLOBAL") == null){
			insertPreference(new Preference("TAM_ENABLED", "false", "Enable the TAM Integration","GLOBAL"));
		}
		if( getPreferenceByNameScope("USER_ADD_AFTER_LOGIN", "GLOBAL") == null){
			insertPreference(new Preference("USER_ADD_AFTER_LOGIN", "false", "Adding user if does't exist","GLOBAL"));
		}
		
		if( getPreferenceByNameScope("ENABLE_LDAP", "GLOBAL") == null){
			insertPreference(new Preference("ENABLE_LDAP", "false", "Enable the LDAP Integration","GLOBAL"));
		}
		
		
		if( getPreferenceByNameScope("APP_SERVER_PREFIX", "GLOBAL") == null){
			insertPreference(new Preference("APP_SERVER_PREFIX", "", "Prefix to identify the App Name from JVM Name","GLOBAL"));
		}
		if( getPreferenceByNameScope("APP_SERVER_SUFFIX", "GLOBAL") == null){
			insertPreference(new Preference("APP_SERVER_SUFFIX", "", "Suffix to identify the App Name from JVM Name.e.g., _M(.*), SERVER_M1, SERVER_M2","GLOBAL"));
		}
		if( getPreferenceByNameScope("APP_GROUPS_PARSE_PATTERN", "GLOBAL") == null){
			insertPreference(new Preference("APP_GROUPS_PARSE_PATTERN", "", "Group Format to indentify. default is restart_(.*). e.g restart_RSA. RSA will be App Name.","GLOBAL"));
		}
		
		if( getPreferenceByNameScope("ADMIN_GROUPS", "GLOBAL") == null){
			insertPreference(new Preference("ADMIN_GROUPS", "", "Admin Console Group..e.g WebSphereConsoleUser, seperate by Comma(,)","GLOBAL"));
		}
		if( getPreferenceByNameScope("SSL_CONFIG_PROP", "GLOBAL") == null){
			insertPreference(new Preference("SSL_CONFIG_PROP", "", "Default SSL Configuration File","GLOBAL"));
		}
		if( getPreferenceByNameScope("SOAP_CONFIG_PROP", "GLOBAL") == null){
			insertPreference(new Preference("SOAP_CONFIG_PROP", "", "Default SOAP Configuration File","GLOBAL"));
		}
		
		if( getPreferenceByNameScope("TRUST_STORE", "GLOBAL") == null){
			insertPreference(new Preference("TRUST_STORE", "", "Default Trust  File","GLOBAL"));
		}
		if( getPreferenceByNameScope("TRUST_STORE_PASSWORD", "GLOBAL") == null){
			insertPreference(new Preference("TRUST_STORE_PASSWORD", "", "Default Trust File password. Please use Admin Utils page to encypt the password","GLOBAL"));
		}
		if( getPreferenceByNameScope("KEY_STORE", "GLOBAL") == null){
			insertPreference(new Preference("KEY_STORE", "", "Default Key Store File","GLOBAL"));
		}
		if( getPreferenceByNameScope("KEY_STORE_PASSWORD", "GLOBAL") == null){
			insertPreference(new Preference("KEY_STORE_PASSWORD", "", "Default Key Store File password. Please use Admin Utils page to encypt the password","GLOBAL"));
		}
		if( getPreferenceByNameScope("DISABLE_WAS_NOTIFICATION", "GLOBAL") == null){
			insertPreference(new Preference("DISABLE_WAS_NOTIFICATION", "false", "Disable the websphere notifiation","GLOBAL"));
		}
		if( getPreferenceByNameScope("WAS_WSADMIN_CLASSPATH", "GLOBAL") == null){
			insertPreference(new Preference("WAS_WSADMIN_CLASSPATH", "", "Classpath Entries for WebSphere wsadmin script","GLOBAL"));
		}
		if( getPreferenceByNameScope("WAS_INSTALL_PATH", "GLOBAL") == null){
			insertPreference(new Preference("WAS_INSTALL_PATH", "", "WebSphere installation path","GLOBAL"));
		}
		if( getPreferenceByNameScope("WAS_CONNECTION_WAIT_TIME", "GLOBAL") == null){
			insertPreference(new Preference("WAS_CONNECTION_WAIT_TIME", "1800", "WebSphere Connection Wait Time","GLOBAL"));
		}
		if( getPreferenceByNameScope("WAS_CONNECTION_RETRY", "GLOBAL") == null){
			insertPreference(new Preference("WAS_CONNECTION_RETRY", "3", "WebSphere Connection Retry Attemp","GLOBAL"));
		}
		
		if( getPreferenceByNameScope("HTTP_REQUEST_MAX", "GLOBAL") == null){
			insertPreference(new Preference("HTTP_REQUEST_MAX", "", "HTTP Server Maximum Request Threshold","GLOBAL"));
		}
		if( getPreferenceByNameScope("HTTP_APP_LIST", "GLOBAL") == null){
			insertPreference(new Preference("HTTP_APP_LIST", "", "HTTP Application Lists,Separeted by comma(,)","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_ENABLED", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_ENABLED", "FALSE", "SMTP Email Enabled","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_HOSTNAME", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_HOSTNAME", "", "SMTP Hostname","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_PORT", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_PORT", "465", "SMTP Port","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_AUTH_ENABLED", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_AUTH_ENABLED", "FALSE", "SMTP Enabled Authentication","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_AUTH_USERNAME", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_AUTH_USERNAME", "", "SMTP Authentication username","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_AUTH_PASSWORD", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_AUTH_PASSWORD", "", "SMTP Authentication password","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_FROM_ADDRESS", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_FROM_ADDRESS", "", "SMTP from Address","GLOBAL"));
		}
		if( getPreferenceByNameScope("SMTP_TO_ADDRESS", "GLOBAL") == null){
			insertPreference(new Preference("SMTP_TO_ADDRESS", "", "SMTP to Address","GLOBAL"));
		}
		
	}
}
